package com.example.a2032.Utils;

import com.example.a2032.constant.DeviceConst;
import com.example.a2032.entity.WatchUserInfo;

import java.util.Calendar;

public class BleSdk {
    public static byte[] addCmdHead(byte[] value) {
        crcValue(value);
        int length = value.length;
        byte[] sendData = new byte[length + 6];
        sendData[0] = 0x4A;
        sendData[1] = 0x43;
        sendData[2] = (byte) 0xAA;
        sendData[3] = (byte) 0xAA;
        sendData[4] = (byte) length;
        sendData[sendData.length - 1] = getBleCrcValue(value);
        System.arraycopy(value, 0, sendData, 5, length);
        return sendData;
    }

    /**
     * @param type   1休眠，2低电，3充电
     * @param enable
     * @return
     */
    public static byte[] sendLowBatteryStatus(int type, boolean enable) {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_LowBattery;
        value[type] = (byte) (enable ? 0x81 : 0x80);
        return addCmdHead(value);
    }

    public static byte getBleCrcValue(byte[] value) {
        byte crc = 0;
        for (int i = 0; i < value.length; i++) {
            crc += value[i];
        }
        return (byte) (crc & 0xff);
    }

    public static void crcValue(byte[] value) {
        byte crc = 0;
        for (int i = 0; i < value.length - 1; i++) {
            crc += value[i];
        }
        value[value.length - 1] = (byte) (crc & 0xff);
    }

    public static byte[] setDeviceTime() {
        byte[] value = new byte[16];
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        value[0] = DeviceConst.CMD_SET_TIME;
        value[1] = ResolveUtil.timeString2Hex(year);
        value[2] = ResolveUtil.timeString2Hex(month);
        value[3] = ResolveUtil.timeString2Hex(day);
        value[4] = ResolveUtil.timeString2Hex(hour);
        value[5] = ResolveUtil.timeString2Hex(min);
        value[6] = ResolveUtil.timeString2Hex(second);
        return addCmdHead(value);
    }

    public static byte[] getDeviceTime() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_GET_TIME;

        return addCmdHead(value);
    }

    public static byte[] enterDfu() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_EnterDfu;

        return addCmdHead(value);
    }

    public static byte[] enablePPGMeasure(int mode, boolean enable, int time) {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_PPGControl;
        value[1] = (byte) mode;
        value[2] = (byte) (enable ? 1 : 0);
        value[3] = (byte) time;
        return addCmdHead(value);
    }

    public static byte[] enableHrvMeasure(boolean enable, int time) {
        return enablePPGMeasure(1, enable, time);
    }

    public static byte[] enableHeartMeasure(boolean enable, int time) {
        return enablePPGMeasure(2, enable, time);
    }

    public static byte[] enableSpo2Measure(boolean enable) {
        return enablePPGMeasure(3, enable, 2);
    }

    public static byte[] enableSpo2Measure(boolean enable, int time) {
        return enablePPGMeasure(3, enable, time);
    }

    public static byte[] enableEcgMeasure(boolean enable, boolean enableData) {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_EnterEcg;
        value[1] = (byte) (enable ? 1 : 0);
        value[2] = (byte) (enableData ? 1 : 0);
        return addCmdHead(value);
    }

    public static byte[] disableEcgMeasure() {
        byte[] value = new byte[16];
        value[0] = 0;
        value[1] = 0;
        value[2] = 0;
        return addCmdHead(value);
    }

    public static byte[] disableSerialPort() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_DisableSerialPort;
        return addCmdHead(value);
    }

    /**
     * 实时计步
     *
     * @param enable     实时计步开关true开启，false关闭
     * @param tempEnable 实时温度开关true开启，false关闭
     * @return
     */
    public static byte[] enableRealTimeStep(boolean enable, boolean tempEnable) {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Enable_Activity;
        value[1] = (byte) (enable ? 1 : 0);
        //value[2] = (byte) (tempEnable ? 1 : 0);
        return addCmdHead(value);
    }

    public static byte[] getTempData() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_GetTemp;
        value[1] = 1;
        //value[2] = (byte) (tempEnable ? 1 : 0);
        return addCmdHead(value);
    }

    public static byte[] getDeviceMac() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Get_Address;
        return addCmdHead(value);
    }

    public static byte[] getSleepData() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Get_SleepData;
        return addCmdHead(value);
    }
    public static byte[] getAllSleepData() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Get_SleepData;
        value[1] = (byte) 0x95;
        return addCmdHead(value);
    }

    public static byte[] getDeviceVersion() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Get_Version;
        return addCmdHead(value);
    }

    public static byte[] getWatchUserInfo() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_GET_USERINFO;
        return addCmdHead(value);
    }

    public static byte[] setWatchUserInfo(WatchUserInfo watchUserInfo) {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Set_UseInfo;
        value[1] = (byte) watchUserInfo.getGender();
        value[2] = (byte) watchUserInfo.getAge();
        value[3] = (byte) watchUserInfo.getHeight();
        value[4] = (byte) watchUserInfo.getWeight();
        value[5] = (byte) watchUserInfo.getStrideLength();
        return addCmdHead(value);
    }

    /**
     * 设置设备id
     *
     * @param deviceId
     * @return
     */
    public static byte[] setDeviceId(String deviceId) {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Set_DeviceID;
        for (int i = 0; i < 6; i++) {
            value[i + 1] = (byte) deviceId.charAt(i);
        }

        return addCmdHead(value);
    }

    /**
     * 恢复出厂设置
     *
     * @return
     */
    public static byte[] reset() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Reset;
        return addCmdHead(value);
    }

    /**
     * 重启设备
     *
     * @return
     */
    public static byte[] MCUReset() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Mcu_Reset;
        return addCmdHead(value);
    }

    /**
     * 进入dfu模式
     *
     * @return
     */
    public static byte[] startOTA() {
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Start_Ota;
        return addCmdHead(value);
    }
}
